package com.xintonghua.bussiness.ui.fragment.cube;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xintonghua.base.utils.DividerGridItemDecoration;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.StockAdapter;
import com.xintonghua.bussiness.base.BaseFragment;
import com.xintonghua.bussiness.bean.CubeBean;
import com.xintonghua.bussiness.bean.ReserveBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 库存
 * 作者：wanxianyi
 * 时间：2018/1/11.
 * 包名：com.xintonghua.bussiness.ui.fragment.cube
 */

public class RepertoryFragment extends BaseFragment {
    @BindView(R.id.lv_produce)
    RecyclerView lvProduce;
    @BindView(R.id.lv_sales)
    RecyclerView lvSales;
    Unbinder unbinder;
    StockAdapter adapter, stockAdapter;
    @BindView(R.id.tv_lable_1)
    TextView tvLable1;
    @BindView(R.id.tv_lable_2)
    TextView tvLable2;
    @BindView(R.id.tv_all_atock)
    TextView tvAllAtock;
    @BindView(R.id.tv_lable_3)
    TextView tvLable3;
    @BindView(R.id.tv_kucunjine)
    TextView tvKucunjine;
    @BindView(R.id.tv_jinghuoshu)
    TextView tvJinghuoshu;
    @BindView(R.id.tv_zhichu)
    TextView tvZhichu;
    @BindView(R.id.tv_xiaohuo)
    TextView tvXiaohuo;
    @BindView(R.id.tv_xiaohuo2)
    TextView tvXiaohuo2;
    @BindView(R.id.tv_time_saixuan)
    TextView tv_time_saixuan;
    Unbinder unbinder1;
    private List<CubeBean> cubeBeanList, cubeBeanList1;

    @Override
    public int getLayout() {
        return R.layout.fragment_repertory;
    }

    @Override
    public void initUI(View view) {
        unbinder = ButterKnife.bind(this, view);
        cubeBeanList = new ArrayList<>();
        cubeBeanList1 = new ArrayList<>();
        getNetData();

    }


    public void getNetData() {
        httpCent.getReserve(getArguments().getInt(BuildConfig.FLAVOR), "", "", this, MyState.requestOne);
    }

    public void initLianggeList() {
        adapter = new StockAdapter(getActivity(), cubeBeanList);
        adapter.setType(2);
        lvProduce.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        lvProduce.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvProduce.setAdapter(adapter);
        stockAdapter = new StockAdapter(getActivity(), cubeBeanList1);
        stockAdapter.setType(1);
        lvSales.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        lvSales.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        lvSales.setAdapter(stockAdapter);

    }


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    bean = JsonUtil.getEntityByJsonString(data.toString(), ReserveBean.class);
                    initData();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.tv_time_saixuan, R.id.tv_xiaohuo, R.id.tv_jinghuoshu})
    public void onClick(View view) {
        Bundle bundle;
        switch (view.getId()) {
            case R.id.tv_time_saixuan:
                bundle = new Bundle();
                bundle.putInt(BuildConfig.FLAVOR, getArguments().getInt(BuildConfig.FLAVOR));
                MyUtils.openActivity(getActivity(), RepertoryActivity.class, bundle);
                // MyUtils.openActivity(getActivity(), RepertoryActivity.class);
                break;
            case R.id.tv_xiaohuo:
                bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, "销货");
                MyUtils.openActivity(getActivity(), RecordActivity.class, bundle);
                break;
            case R.id.tv_jinghuoshu:
                bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, "进货");
                MyUtils.openActivity(getActivity(), RecordActivity.class, bundle);
                break;
        }
    }

    ReserveBean bean;

    public void initData() {
        tvAllAtock.setText("" + bean.getTotalCount());
        tvKucunjine.setText("" + bean.getTotalPrice());

        tvJinghuoshu.setText("进货+（" + bean.getTotalJinduoCount() + "瓶)");
        tvZhichu.setText("-" + bean.getTotalMoney() + "元");
        tvXiaohuo.setText("销货+（" + bean.getXlCount() + "瓶)");
        tvXiaohuo2.setText("+" + bean.getXlMoney() + "元");
        List<ReserveBean.JinhuoListBean> jing_list = bean.getJinhuoList();
        List<ReserveBean.XiaoliangListBean> xiao_list = bean.getXiaoliangList();

        cubeBeanList = new ArrayList<>();
        cubeBeanList1 = new ArrayList<>();
        for (int i = 0; i < jing_list.size(); i++) {
            cubeBeanList.add(new CubeBean(R.drawable.cube_product, "", jing_list.get(i).getGoods_name(), jing_list.get(i).getTotalPrice(), jing_list.get(i).getCount(), false));
        }
        for (int i = 0; i < xiao_list.size(); i++) {
            cubeBeanList1.add(new CubeBean(R.drawable.cube_product, "", xiao_list.get(i).getName(), xiao_list.get(i).getTotalPrice(), xiao_list.get(i).getCount(), false));
        }


        initLianggeList();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
